using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using CentralOffice.Admin.DataAccess;

namespace CentralOffice.Admin.BusinessActions
{
    public class Role
    {
        public static DataTable GetUsersForRole()
        {
            DataTable dtRoles = RoleAdapter.GetUsersForRole();
            if (dtRoles == null) return null;

            DataTable dt = new DataTable();
            //dt.Columns.Add("UserId", typeof(System.Guid));
            //dt.Columns.Add("UserName", typeof(System.String));
            //dt.Columns.Add("UserType", typeof(System.Int32));
            //dt.Columns.Add("EmployeeId", typeof(System.String));
            //dt.Columns.Add("Name", typeof(System.String));
            dt.Columns.Add("RoleId", typeof(System.Guid));
            dt.Columns.Add("RoleName", typeof(System.String));
            dt.Columns.Add("OrderNumber", typeof(System.Int32));
            dt.Columns.Add("UserId", typeof(System.String));
            dt.Columns.Add("UserName", typeof(System.String));
            dt.Columns.Add("Name", typeof(System.String));

            Guid roleId = Guid.Empty;
            DataRow current = null;
            
            foreach(DataRow row in dtRoles.Rows)
            {
                if (row["UserId"] == DBNull.Value)
                {
                    roleId = (Guid)row["RoleId"];
                    current = dt.NewRow();
                    current["RoleId"] = row["RoleId"];
                    current["RoleName"] = row["RoleName"];
                    current["OrderNumber"] = row["OrderNumber"];
                    current["UserName"] = "";
                    current["Name"] = "";
                    dt.Rows.Add(current);

                    continue;
                }
                if (row["RoleId"].Equals(roleId))
                {
                    current["UserName"] = current["UserName"].ToString() + "," + row["UserName"].ToString();
                    current["Name"] = current["Name"].ToString() + "," + row["Name"].ToString();
                }
                else
                {
                    roleId = (Guid)row["RoleId"];
                    current = dt.NewRow();
                    current["RoleId"] = row["RoleId"];
                    current["RoleName"] = row["RoleName"];
                    current["OrderNumber"] = row["OrderNumber"];
                    current["UserName"] = row["UserName"];
                    current["Name"] = row["Name"];
                    dt.Rows.Add(current);
                }
            }

            return dt;
        }
    }
}
